package org.example.httpdemo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.example.pojo.User;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


public class HttpTests {

    CloseableHttpClient httpClient;

    @Before  //初始化
    public void init() {
        httpClient = HttpClients.createDefault();
    }

    @Test
    public void testGet() throws IOException {
        //发起get请求
        HttpGet request = new HttpGet("http://www.baidu.com");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

    @Test
    public void testPost() throws IOException {
        //发起Post请求
        HttpGet request = new HttpGet("http://www.oschina.net/");
        request.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

    /****
     * 请求另外一个程序
     * @throws IOException
     */
    @Test
    public void testGetPojo() throws IOException {
        HttpGet request = new HttpGet("http://localhost:8088/hello");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

    @Test
    public void getPojo() throws IOException {
        HttpGet request = new HttpGet("http://localhost:8088/getUSer");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }


    private ObjectMapper mapper = new ObjectMapper();
    /****
     *  json处理工具  把对象转换成字符串
     */
    @Test
    public void testJson() throws JsonProcessingException {
        User user = new User();
        user.setId(8L);
        user.setUsername("柳岩");
        user.setPassword("123456");
        user.setPhone("22222");
        user.setCreated(new Date());
        // 序列化
        String json = mapper.writeValueAsString(user);
        System.out.println("json = " + json);
        //{"id":8,"username":"柳岩","password":"123456","phone":"22222","created":1702623753192}
    }

    /****
     * 把json字符串转换成对象
     * @throws IOException
     */
    @Test
    public void testToObj() throws IOException {
        String str="{\"id\":8,\"username\":\"柳岩\",\"password\":\"123456\",\"phone\":\"22222\",\"created\":1702623753192}";
        User  user=  mapper.readValue(str,User.class);
        System.out.println("user:"+user);

    }

    /****
     * json处理集合
     * @throws IOException
     */
    @Test
    public void testList() throws IOException {
        User user = new User();
        user.setId(8L);
        user.setUsername("柳岩");
        user.setPhone("1111");
        user.setCreated(new Date());
        user.setPassword("2222");
        // 序列化,得到对象集合的json字符串    把两个对象放进去
        String json = mapper.writeValueAsString(Arrays.asList(user, user));
        System.out.println("json:"+json);
        // 反序列化，接收两个参数：json数据，反序列化的目标类字节码
        List<User> users = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, User.class));
        for (User u : users) {
            System.out.println("u = " + u);
        }
    }


    /***
     * 当对象泛型关系复杂时，类型工厂也不好使了。这个时候Jackson提供了TypeReference来接收类型泛型，然后底层通过反射来获取泛型上的具体类型。实现数据转换。
     * @throws IOException
     */
    @Test
    public void testObj() throws IOException {
        User user = new User();
        user.setId(8L);
        user.setUsername("柳岩");
        user.setPhone("1111");
        user.setCreated(new Date());
        user.setPassword("2222");

        // 序列化,得到对象集合的json字符串
        String json = mapper.writeValueAsString(Arrays.asList(user, user));
        System.out.println("json:"+json);
        // 反序列化，接收两个参数：json数据，反序列化的目标类字节码
        List<User> users = mapper.readValue(json, new TypeReference<List<User>>(){});
        for (User u : users) {
            System.out.println("u = " + u);
        }
    }

}